/*
Copyright (c) 2009 Hayden Curran <haydenz@gmail.com>
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
        this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
        this list of conditions and the following disclaimer in the
        documentation and/or other materials provided with the distribution.
    * The names of its contributors may not be used to endorse or promote
        products derived from this software without specific prior
        written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "buffer.h"

/*
    Initialises the buffer_t struct
*/
int buffer_init(buffer_t *buffer) {
    buffer->start = buffer->buf;
    buffer->end = buffer->start;
    return 1;
}

/*
    Adds buf to the end of our buffer
    Returns len, or
    Returns -1 if not enough space to store it
*/
int buffer_receive(buffer_t *buffer, char buf[], int len) {
    int cutlen;

    if (buffer_length(buffer) + len >= BUFFER_LENGTH)
        return -1; // Ran out of buffer space

    if ((buffer->end + len) >= (buffer->buf + BUFFER_LENGTH)) {
        // Buffer has to wrap around to start

        cutlen = (buffer->buf + BUFFER_LENGTH) - buffer->end;
        memcpy(buffer->end, buf, cutlen);
        memcpy(buffer->buf, (buf + cutlen), len - cutlen);

        buffer->end = buffer->buf + (len - cutlen);
    }
    else {
        memcpy(buffer->end, buf, len);
        buffer->end += len;
    }

    return len;
}

/*
    Removes 1 packet from the buffer and puts it into packet
    Returns the length of the returned packet
    If the packet is incomplete returns -1
*/
int buffer_get_packet(buffer_t *buffer, char *packet) {
    int len = 0;
    char *b = buffer->start;
    char *p = packet;

    if (buffer->start == buffer->end) return 0;

    // We have to wrap around
    if (buffer->start > buffer->end) {
        // loop from start of circular buffer to end of our char array, buf
        // or stop at \r
        while (b < (buffer->buf + BUFFER_LENGTH) &&
               *b != '\r')
        {
            *p++ = *b++;
            len++;
        }

        // Set b to start of our char array
        // when we get to the next while loop it won't execute since *b == '\r'
        // But if *b isn't '\r' then we loop until end of circular buffer
        if (*b != '\r') b = buffer->buf;
    }

    // loop until end of circular buffer or to '\r'
    while (b < buffer->end &&
            *b != '\r')
    {
        *p++ = *b++;
        len++;
    }

    // if *b isn't '\r' then we have an incomplete packet and so return -1
    if (*b != '\r') return -1;

    *p = '\0'; // null terminate the packet
    buffer->start = b+2; // skip over \r\n

    return len;
}

/*
    Returns the length of the buffer
*/
int buffer_length(buffer_t *buffer) {
    if (buffer->end > buffer->start) return buffer->end - buffer->start;
    return buffer->start - buffer->end;
}
